package com.kkb.kkbportal.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.kkbcommon.api.CommonPage;
import com.kkb.kkbcommon.api.CommonResult;
import com.kkb.kkbportal.domain.ums.Role;
import com.kkb.kkbportal.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 天选ywy
 */
@RestController
@Api(tags = "roleController", description = "角色管理")
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "角色信息分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "conditionalRole", value = "角色筛选条件", dataType = "Role"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页页数", defaultValue = "10", dataType = "int")
    })
    @PostMapping("/query/page")
    public CommonResult<CommonPage<Role>> listPage(@RequestBody Role conditionalRole,
                                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                                   @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Role> page = roleService.selectPage(conditionalRole, pageNum, pageSize);
        CommonPage<Role> commonPage = CommonPage.restPage(page.getList());
        return CommonResult.success(commonPage, "查询成功");
    }

    @ApiOperation(value = "根据id得到角色信息")
    @ApiImplicitParam(name = "id", value = "角色id", dataType = "long")
    @GetMapping("/{id}}")
    public CommonResult<Role> selectRoleById(@PathVariable("id") Long id) {
        Role role = roleService.selectRoleById(id);
        return CommonResult.success(role, "查询成功");
    }

    @ApiOperation(value = "新增角色")
    @ApiImplicitParam(name = "role", value = "角色对象")
    @PutMapping("/add")
    public CommonResult<Role> addRole(@RequestBody Role role) {
        roleService.insert(role);
        return CommonResult.success(null, "添加成功");
    }

    @ApiOperation(value = "根据角色id修改角色")
    @ApiImplicitParam(name = "role", value = "角色对象")
    @PutMapping("/update")
    public CommonResult<Role> updateRoleById(@RequestBody Role role) {
        boolean update = roleService.updateById(role);
        if (roleService.updateById(role)) {
            return CommonResult.success(null, "修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @ApiOperation(value = "根据角色id进行逻辑删除", notes = "角色删除接口")
    @ApiImplicitParam(name = "id", value = "角色id", dataType = "long")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Role> deleteRoleById(@PathVariable("id") Long id) {
        if (roleService.deleteById(id)) {
            return CommonResult.success(null, "删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    @ApiOperation(value = "角色信息列表查询", notes = "角色列表查询接口")
    @ApiImplicitParam(name = "conditionalRole", value = "角色筛选条件", dataType = "Role")
    @PostMapping("/query/list")
    public CommonResult<List<Role>> list(@RequestBody Role conditionalRole) {
        List<Role> page = roleService.selectByCondition(conditionalRole);
        System.out.println(page);
        return CommonResult.success(page, "查询成功");
    }
}
